---
title: "Manuscript Figures and Tables"
subtitle: "Acculturation Orientations and Latino Political Behavior"
author: "Jessala A. Grijalva"
date: "`r format(Sys.Date(), '%B %d, %Y')`"
format:
  pdf:
    toc: true
    toc-depth: 2
    number-sections: true
    geometry: margin=1in
    colorlinks: true
    keep-tex: false
execute:
  warning: false
  message: false
  echo: false
---

```{r setup}
#| label: setup

library(here)
library(pacman)
p_load(tidyverse, knitr, kableExtra)

# ── Publishing Standards ──
VAR_LABELS <- c(
  "IDEOLOGY"  = "Ideology",
  "PARTYID"   = "Party Identification",
  "IMMVIEW"   = "Immigration View",
  "DREAMACT"  = "DREAM Act Support",
  "IMMPOLICY" = "Immigration Policy"
)
dvs <- c("IDEOLOGY", "PARTYID", "IMMVIEW", "DREAMACT", "IMMPOLICY")
label_vars <- function(x) unname(VAR_LABELS[x])

# ── Load precomputed Phase 3 results ──
# (Render Phase_3_Political_Profiles.qmd first to generate this file)
load(here("data", "processed", "phase3_results.rda"))

# ── Visual Standards ──
ORIENTATION_FILLS <- c(
  "Culture Affirming" = "gray15",
  "Assimilationist"   = "gray40",
  "Demicultural"      = "gray65",
  "Bicultural"        = "gray85"
)
ORIENTATION_SHAPES <- c(
  "Culture Affirming" = 16,
  "Assimilationist"   = 17,
  "Demicultural"      = 15,
  "Bicultural"        = 18
)
ORIENTATION_LINETYPES <- c(
  "Culture Affirming" = "solid",
  "Assimilationist"   = "dashed",
  "Demicultural"      = "dotted",
  "Bicultural"        = "dotdash"
)
```

\newpage

# Tables

## Table 1: Summary Political Profiles

```{r tbl-summary-profiles}
#| label: tbl-summary-profiles
#| tbl-cap: "Summary Political Profiles by Acculturation Orientation"

summary_profiles %>%
  mutate(across(starts_with("pct"), ~ round(.x, 1))) %>%
  kable(booktabs = TRUE,
        col.names = c("Orientation", "n", "% Moderate", "% Democrat",
                       "% Pro-Imm.", "% DREAM", "% Permissive"),
        align = c("l", rep("r", 6))) %>%
  kable_styling(latex_options = c("hold_position", "scale_down"),
                full_width = FALSE)
```

\noindent \textit{Note:} $N$ = `r n_total`. Pro-Imm. = views immigrants as strengthening the country. DREAM = supports DREAM Act ($\geq$ 3 on 1--4 scale). Permissive = favors permissive immigration policy ($\geq$ 4 on 1--5 scale). Orientations derived from GMM $k$ = 4 solution.

## Table 2: Omnibus Tests

```{r tbl-kruskal-wallis}
#| label: tbl-kruskal-wallis
#| tbl-cap: "Omnibus Test Results (Kruskal-Wallis)"

kw_results %>%
  mutate(
    Variable = label_vars(Variable),
    Chi_sq = round(Chi_sq, 2),
    p_value = ifelse(p_value < 0.001, "< .001", sprintf("%.3f", p_value))
  ) %>%
  kable(booktabs = TRUE,
        col.names = c("Variable", "$\\chi^2$", "df", "$p$"),
        escape = FALSE,
        align = c("l", "r", "r", "r")) %>%
  kable_styling(latex_options = c("hold_position"), full_width = FALSE)
```

\noindent \textit{Note:} Kruskal-Wallis rank-sum tests with $k$ = 4 orientations. df = 3 for all tests.

## Table 3: Dimension Contrasts

```{r tbl-dimension-contrasts}
#| label: tbl-dimension-contrasts
#| tbl-cap: "Dimension Contrasts: Heritage vs American"

dimension_results %>%
  mutate(
    Variable = label_vars(Variable),
    across(c(Delta, CI_low, CI_high), ~ round(.x, 2)),
    CI = paste0("[", CI_low, ", ", CI_high, "]")
  ) %>%
  select(Dimension, Variable, Delta, CI, Significant) %>%
  arrange(Dimension, Variable) %>%
  kable(booktabs = TRUE,
        col.names = c("Dimension", "Variable", "$\\delta$", "95\\% CI",
                       "Significant"),
        escape = FALSE,
        align = c("l", "l", "r", "c", "c")) %>%
  kable_styling(latex_options = c("hold_position", "scale_down"),
                full_width = FALSE)
```

\noindent \textit{Note:} Cliff's $\delta$ with 95\% bootstrap CIs ($B$ = `r format(n_boot, big.mark = ",")`, seed = `r SEED`). Heritage: Culture Affirming + Bicultural vs. Assimilationist + Demicultural. American: Assimilationist + Bicultural vs. Culture Affirming + Demicultural.

## Table 4: Association Strength

```{r tbl-cramers-v}
#| label: tbl-cramers-v
#| tbl-cap: "Association Strength (Cram\\'{e}r's V)"

cramers_results %>%
  mutate(
    Variable = label_vars(Variable),
    across(c(Cramers_V, CI_low, CI_high), ~ round(.x, 2)),
    CI = paste0("[", CI_low, ", ", CI_high, "]")
  ) %>%
  select(Variable, Cramers_V, CI) %>%
  kable(booktabs = TRUE,
        col.names = c("Variable", "Cram\\'{e}r's V", "95\\% CI"),
        escape = FALSE,
        align = c("l", "r", "c")) %>%
  kable_styling(latex_options = c("hold_position"), full_width = FALSE)
```

\noindent \textit{Note:} Cram\'{e}r's V with 95\% bootstrap CIs. Reference thresholds: 0.10 (small), 0.30 (medium), 0.50 (large).

\newpage

# Figures

## Figure 1: Political Profiles

```{r fig-profiles}
#| label: fig-profiles
#| fig-cap: "Political Profiles by Acculturation Orientation"
#| fig-width: 10
#| fig-height: 6

profile_long <- summary_profiles %>%
  pivot_longer(cols = starts_with("pct"), names_to = "measure",
               values_to = "percentage") %>%
  mutate(
    measure = case_when(
      measure == "pct_moderate" ~ "% Moderate",
      measure == "pct_democrat" ~ "% Democrat",
      measure == "pct_pro_immigrant" ~ "% Pro-Immigrant",
      measure == "pct_dream_support" ~ "% DREAM Support",
      measure == "pct_permissive" ~ "% Permissive"
    ),
    measure = factor(measure, levels = c("% Moderate", "% Democrat",
                                          "% Pro-Immigrant", "% DREAM Support",
                                          "% Permissive"))
  )

p1 <- ggplot(profile_long, aes(x = measure, y = percentage, fill = cluster)) +
  geom_col(position = position_dodge(width = 0.8), width = 0.7,
           color = "black", linewidth = 0.3) +
  geom_text(aes(label = round(percentage, 0)),
            position = position_dodge(width = 0.8), vjust = -0.5, size = 3) +
  scale_fill_manual(values = ORIENTATION_FILLS) +
  scale_y_continuous(limits = c(0, 100), expand = c(0, 0)) +
  labs(x = NULL, y = "Percentage", fill = "Orientation") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "bottom")
p1

ggsave(here("figures", "fig1-profiles.png"), p1, width = 10, height = 6, dpi = 600)
```

## Figure 2: Pairwise Effect Sizes

```{r fig-pairwise}
#| label: fig-pairwise
#| fig-cap: "Pairwise Effect Sizes by Political Outcome"
#| fig-width: 12
#| fig-height: 8

pairwise_plot <- pairwise_results %>%
  mutate(Variable = label_vars(Variable),
         Comparison = paste0(substr(Group1, 1, 3), " vs ", substr(Group2, 1, 3)))

p2 <- ggplot(pairwise_plot, aes(x = Delta, y = Comparison, color = Significant)) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "gray50") +
  geom_errorbarh(aes(xmin = CI_low, xmax = CI_high), height = 0.2) +
  geom_point(size = 2) +
  scale_color_manual(values = c("Yes" = "black", "No" = "gray60")) +
  facet_wrap(~ Variable, ncol = 5) +
  labs(x = expression("Cliff's " * delta), y = NULL, color = "Significant") +
  theme_minimal() +
  theme(legend.position = "bottom", strip.text = element_text(face = "bold"))
p2

ggsave(here("figures", "fig2-pairwise.png"), p2, width = 12, height = 8, dpi = 600)
```

## Figure 3: Association Strength

```{r fig-cramers}
#| label: fig-cramers
#| fig-cap: "Association Strength by Political Outcome"
#| fig-width: 8
#| fig-height: 5

cramers_plot <- cramers_results %>%
  mutate(Variable = label_vars(Variable))

p3 <- ggplot(cramers_plot, aes(x = reorder(Variable, Cramers_V), y = Cramers_V)) +
  geom_col(fill = "gray40", width = 0.6) +
  geom_errorbar(aes(ymin = CI_low, ymax = CI_high), width = 0.2) +
  geom_hline(yintercept = c(0.1, 0.3), linetype = "dashed",
             color = "gray60", alpha = 0.7) +
  coord_flip() +
  labs(x = NULL, y = expression("Cram\u00E9r's V")) +
  theme_minimal()
p3

ggsave(here("figures", "fig3-cramers.png"), p3, width = 8, height = 5, dpi = 600)
```

## Figure 4: Dimension Effects

```{r fig-dimensions}
#| label: fig-dimensions
#| fig-cap: "Dimension Effects Comparison (Heritage vs American)"
#| fig-width: 9
#| fig-height: 5

dimension_plot <- dimension_results %>%
  mutate(Variable = label_vars(Variable))

p4 <- ggplot(dimension_plot, aes(x = Variable, y = Delta, fill = Dimension)) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "gray50") +
  geom_col(position = position_dodge(width = 0.8), width = 0.7,
           color = "black", linewidth = 0.3) +
  geom_errorbar(aes(ymin = CI_low, ymax = CI_high),
                position = position_dodge(width = 0.8), width = 0.2) +
  scale_fill_manual(values = c("Heritage" = "gray30", "American" = "gray70")) +
  labs(x = NULL, y = expression("Cliff's " * delta * " (High vs Low)")) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "bottom")
p4

ggsave(here("figures", "fig4-dimensions.png"), p4, width = 9, height = 5, dpi = 600)
```

## Figure 5: Quadrant Map

```{r fig-quadrant}
#| label: fig-quadrant
#| fig-cap: "Acculturation Orientations in Two-Dimensional Space"
#| fig-width: 7
#| fig-height: 6

quadrant_data <- tibble(
  cluster = factor(
    c("Culture Affirming", "Assimilationist", "Bicultural", "Demicultural"),
    levels = levels(df$cluster)
  ),
  Heritage = c(1, 0, 1, 0), American = c(0, 1, 1, 0)
) %>%
  mutate(x = ifelse(American == 1, 0.75, 0.25),
         y = ifelse(Heritage == 1, 0.75, 0.25)) %>%
  left_join(cluster_ns %>% select(cluster, n), by = "cluster")

p5 <- ggplot(quadrant_data, aes(x = x, y = y)) +
  geom_rect(aes(xmin = 0, xmax = 0.5, ymin = 0.5, ymax = 1),
            fill = "gray90", alpha = 0.5) +
  geom_rect(aes(xmin = 0.5, xmax = 1, ymin = 0.5, ymax = 1),
            fill = "gray80", alpha = 0.5) +
  geom_rect(aes(xmin = 0, xmax = 0.5, ymin = 0, ymax = 0.5),
            fill = "gray95", alpha = 0.5) +
  geom_rect(aes(xmin = 0.5, xmax = 1, ymin = 0, ymax = 0.5),
            fill = "gray90", alpha = 0.5) +
  geom_hline(yintercept = 0.5, linetype = "dashed") +
  geom_vline(xintercept = 0.5, linetype = "dashed") +
  geom_point(size = 10, color = "black", fill = "white", shape = 21) +
  geom_text(aes(label = paste0(cluster, "\n(n=", n, ")")),
            size = 3, fontface = "bold") +
  scale_x_continuous(limits = c(0, 1), breaks = c(0.25, 0.75),
                     labels = c("Low", "High"), expand = c(0.05, 0.05)) +
  scale_y_continuous(limits = c(0, 1), breaks = c(0.25, 0.75),
                     labels = c("Low", "High"), expand = c(0.05, 0.05)) +
  labs(x = "American Cultural Orientation",
       y = "Heritage Cultural Orientation") +
  theme_minimal() +
  theme(panel.grid = element_blank(),
        axis.text = element_text(size = 10, face = "bold"))
p5

ggsave(here("figures", "fig5-quadrant.png"), p5, width = 7, height = 6, dpi = 600)
```

\newpage

# Session Info

```{r session-info}
#| label: session-info

sessionInfo()
```
